package gui;

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.*;

import parser.Block;

public class FpDraw extends JPanel {
	
	final int PAD = 20;
	private Map<String, Block> data;

	public FpDraw(Map<String, Block> blocks) {
		data = blocks;
		this.setBackground(Color.white);
	}

	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		int w = getWidth();
		int h = getHeight();

		// draw line
		g2.draw(new Line2D.Double(PAD, h - PAD, w - PAD, h - PAD));

		// Draw labels.
		Font font = g2.getFont();
		FontRenderContext frc = g2.getFontRenderContext();
		LineMetrics lm = font.getLineMetrics("0", frc);
		float sh = lm.getAscent() + lm.getDescent();
		String s = "The Final FloorPlan";
		float sy = h - PAD + (PAD - sh) / 2 + lm.getAscent();
		float sw = (float) font.getStringBounds(s, frc).getWidth();
		float sx = (w - sw) / 2;
		g2.setColor(Color.blue);
		g2.drawString(s, sx, sy);

		/*
		 * BufferedImage image = null;
		 * 
		 * try { image = ImageIO.read(new File("user.dir")); } catch
		 * (IOException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } g2.drawImage(image, 200 , 200, null);
		 */

		/*
		 * // Draw lines. double xInc = (double) (w - 2 * PAD) / (data.size() -
		 * 1); double scale = (double) (h - 2 * PAD) / getMaxY();
		 * g2.setPaint(Color.blue.darker()); for (int i = 0; i < data.length -
		 * 1; i++) { double x1 = PAD + i * xInc; double y1 = h - PAD - scale *
		 * data[i]; double x2 = PAD + (i + 1) * xInc; double y2 = h - PAD -
		 * scale * data[i + 1]; g2.draw(new Line2D.Double(x1, y1, x2, y2)); }
		 */

		// Mark data points.
		double xInc = (double) (w - 2 * PAD) / getMaxX();
		double scale = (double) (h - 2 * PAD) / getMaxY();
		g2.setPaint(Color.red);

		for (Block b : data.values()) {

			double x = h - PAD - xInc * b.getX_coordinate();
			double y = h - PAD - scale * b.getY_coordinate();
			g2.setPaint(Color.red);
			g2.fill(new Rectangle2D.Double(x, y, b.getX_coordinate() * 10, b
					.getY_coordinate() * 10));

		}

	}

	double getMaxY() {
		double maxY = -1;
		for (Block b : data.values()) {
			if (b.getY_coordinate() > maxY) {
				maxY = b.getY_coordinate();
			}
		}
		return maxY;
	}

	double getMaxX() {
		double maxX = -1;
		for (Block b : data.values()) {
			if (b.getX_coordinate() > maxX) {
				maxX = b.getX_coordinate();
			}
		}
		return maxX;
	}
}
